package br.usp.ime.origami.foldcut.gui.canvas;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.swing.JFrame;

import br.usp.ime.origami.foldcut.gui.listener.FirstPhaseDiskPositioningMouseListener;
import br.usp.ime.origami.foldcut.io.Reader;
import br.usp.ime.origami.foldcut.packing.FirstPhaseManualPacker;
import br.usp.ime.origami.foldcut.structures.simplegraph.Graph;
import br.usp.ime.origami.foldcut.structures.simplegraph.Vertex;
import br.usp.ime.origami.model.Point;

public class InputDataCanvas extends Canvas
{
	private List<Vertex> points = new ArrayList<Vertex>();

	
	private Point destinationPoint;

	

	public void addPoint(Point point)
	{
		this.points.add(new Vertex(point));
	}

	@Override
	public void paint(Graphics realg)
	{
		BufferedImage buffer = new BufferedImage(800, 800, BufferedImage.TYPE_INT_ARGB_PRE);
		Graphics2D g = buffer.createGraphics();
		g.setColor(Color.BLACK);
		
		for (int i = 0; i < this.points.size(); i++)
		{

			Point point = points.get(i);
			//g.fillOval((int) (point.getX() +1), (int) (point.getY()+1), 3, 3);
			if (i > 0)
			{
				Point before = points.get(i - 1);
				g.drawLine((int) point.getX(), (int) point.getY(), (int) before.getX(), (int) before.getY());
			}
		}
		if (destinationPoint != null && !points.isEmpty())
		{
			Point lastPoint = points.get(points.size() - 1);
			g.drawLine((int) lastPoint.getX(), (int) lastPoint.getY(), (int) destinationPoint.getX(),
					(int) destinationPoint.getY());
		}

		realg.drawImage(buffer, 0, 0, buffer.getWidth(), buffer.getHeight(), null);
	}
	
	public void draw(Graphics g) {

	}

	public List<? extends Point> getPoints()
	{
		return points;
	}

	public void setDestinationPoint(Point p)
	{
		this.destinationPoint = p;
	}

	public void done()
	{
	
		System.out.println("DONE, starting normal packer");
		Graph graph = new Graph();

		int xmax = 600;
		int ymax = 600;
		
		graph.addComponentPaper(Arrays.asList(new Vertex[] { new Vertex(0, 0), new Vertex(xmax, 0), new Vertex(xmax, ymax),
				new Vertex(0, ymax) }));	
		graph.addComponent(points);
				
		FirstPhaseManualPacker packer = new FirstPhaseManualPacker(graph);
		packer.automaticVertexPacking();

		FirstPhaseCanvas canvas = new FirstPhaseCanvas();
		FirstPhaseDiskPositioningMouseListener listener = new FirstPhaseDiskPositioningMouseListener(packer, canvas);

		
		JFrame frame = new JFrame();

		canvas.setSize(800, 800);
		canvas.addMouseListener(listener);
		canvas.addMouseMotionListener(listener);
		canvas.setVisible(true);
		
		frame.add(canvas);
		frame.setSize(900, 900);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);


	}
}
